package com.andiary.common.note;

import com.andiary.common.R;
import com.andiary.common.database.InitializeDB;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.util.Log;
import android.view.View;




public class SelectImage extends Activity  {
	
	private Uri uriImage;
	
	private ListAdapter mAdapter;
	private ListView list;	
    private ImageButton btnNote;
    private ImageButton btnInfo;
    private ImageView im;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         
         setContentView(R.layout.imageselect);
         
         btnNote = (ImageButton) findViewById (R.id.day);
         btnNote.setOnClickListener(btnNoteListener);

         btnInfo = (ImageButton) findViewById (R.id.info);
         btnInfo.setOnClickListener(btnInfoListener);
         
         im= (ImageView) findViewById (R.id.image);
         
         list = (ListView) this.findViewById(R.id.list);
         
         //set the current image of the note
         populatefields(); 
         
         Cursor c = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,null,
                 null, null,Media.DATE_ADDED);
         startManagingCursor(c);
         
         
 		 mAdapter = new SimpleCursorAdapter (
				this,
				R.layout.imageselect_item,
				c,
				new String[] {Media.TITLE}, // Name for group layouts
				new int[] { R.id.text1}
				);

		 list.setAdapter(mAdapter);
		 list.setOnItemClickListener(listImageListener); 
		 list.setOnItemSelectedListener (new OnItemSelectedListener() {
             public void onItemSelected(
                     AdapterView<?> parent, View view, int position, long id) {
            	 String idImage;
            	//cursor for all images of the list
     		 	Cursor allimages = (Cursor) parent.getItemAtPosition(position);	
     			//ID of selected image of the list
     	        String idofimage = allimages.getString(allimages.getColumnIndex(Media._ID));
     	        
     	       //titleCursor: Cursor for all ID and TITLE in the media
     	         Cursor	titleCursor = managedQuery(Media.EXTERNAL_CONTENT_URI, new String[] {
     						Media._ID, Media.TITLE}, null, null, Media.DATE_ADDED);
     				
     	         boolean findImage = false;
     				if (titleCursor.moveToFirst()) {
     					     //For all TITLE
     					do {
     						//IF for that TITLE  its ID is equal a the ID of selected image 
     						int mIdColumnIndex = titleCursor.getColumnIndex(Media._ID);
     						if (idofimage.equals(titleCursor.getString(mIdColumnIndex))) {
     							findImage = true; //image in titleCursor is found
     							break;
     						}
     					} while (titleCursor.moveToNext());
     				}
     				if (findImage == true) { 
     					idofimage= titleCursor.getString(titleCursor.getColumnIndex(Media._ID));
     				   
     					
     				uriImage=Media.EXTERNAL_CONTENT_URI;
     				uriImage=Uri.withAppendedPath(uriImage,idofimage);
     				}
                     try {     
     				   
                     	im.setImageURI(uriImage);
                     	im.setAdjustViewBounds(true);
                     }catch (Exception e) {
                     		Log.e("error", uriImage.toString(), e);
                     	    Toast.makeText(SelectImage.this, "This image can't be played  ", Toast.LENGTH_LONG).show();
                     			
                     	}

             }

             public void onNothingSelected(AdapterView<?> parent) {  
            	 populatefields();
             } 
         });
		 
		 //
        
    }
    private void populatefields() {
	// TODO Auto-generated method stub
	 //set the image of the current note
    //for that retriveve the URI image of the current note of the DATABASE
     //im.setImageURI(uriCurrentImage);
     //im.setAdjustViewBounds(true);
    	
}
 

    private ListView.OnItemClickListener listImageListener = new ListView.OnItemClickListener() {
		
    	public void onItemClick (AdapterView<?> parent, View v, int position,long id){ 
		 
    			//-----------------------------------------------------------------------
    			/** save uriImage in database */
    		//-----------------------------------------------------------------------	
	 			setResult(RESULT_OK);
        		finish();
		}
    
    };
	private Button.OnClickListener btnNoteListener= new Button.OnClickListener() {

		public void onClick(View view) {
			//-----------------------------------------------------------------------
			/** don't save uriImage in database */
		//-----------------------------------------------------------------------                   
			setResult(RESULT_OK);
			finish();
		}
  
	};

    private Button.OnClickListener btnInfoListener= new Button.OnClickListener() {

    	public void onClick(View view) {
			//-----------------------------------------------------------------------
			/** don't save uriImage in database */
    		/**start activity INFO*/
		//-----------------------------------------------------------------------	
        	//Intent info = new Intent(SelectImage.this,info.Info.class);
    	    //startActivity(info);
    		//finish();
    	
    	}
  
    };  
   
	
}




